﻿###################################################
## Maiden Hunt Contract
## laamp_regula_maiden_hunt
##
## Criminal contract: The player identifies an ongoing activity (feast, wedding,
## tournament, coronation, funeral, hunt, examination, or university visit) as a
## target-rich opportunity and travels there to kidnap women from the guest list in a
## push-your-luck loop. Self-motivated — the player lurks nearby, picking off
## targets one by one with escalating difficulty, earning gold and bandit XP
## for each successful capture. The LAAMP employer is the activity host (target
## of the crime), but the player is not working for anyone — they are exploiting
## the gathering for personal gain.
##
## Variables on the contract:
## target_activity            - reference to the target activity
## activity_type_*            - flag for the activity type (one of 8)
## activity_location          - province where the activity is held
## difficulty_level           - escalating difficulty modifier (init 0)
## gold_success_standard      - pre-calculated gold for standard reward
## gold_success_critical      - pre-calculated gold for critical reward
## kidnap_gold            - base gold per individual kidnapping
## player_arrived             - flag set when player arrives at activity
## special_target_kidnapped   - flag set when special target is captured
##
## Group: laamp_contracts_criminal_group
## Events: regula_maiden_hunt_contract namespace
## Criminal: yes (bandit track gallowsbait XP)
## NO SCHEME — pure event-chain contract
###################################################
laamp_regula_maiden_hunt = {
	group = laamp_contracts_criminal_group
	icon = "gfx/interface/icons/task_contracts/regula_icon_task_contract_maiden_hunt.dds"

	is_criminal = yes
	travel = yes
	use_diplomatic_range = yes

	weight = {
		# Standard weights.
		value = task_contract_weight_default_value
		add = laamp_contracts_weight_up_criminal_value
		add = laamp_contracts_weight_up_regula_value
		add = task_contract_weight_interesting_somewhat_rare_value
		# Employer weights (lax oversight = more opportunity, strong oversight = less).
		scope:employer = {
			add = {
				# Weight up — lax oversight traits (distracted/inattentive hosts).
				if = {
					limit = { has_trait = impatient }
					add = task_contract_weight_bonus_employer_has_associated_traits_value
				}
				if = {
					limit = { has_trait = callous }
					add = task_contract_weight_bonus_employer_has_associated_traits_value
				}
				if = {
					limit = { has_trait = lazy }
					add = task_contract_weight_bonus_employer_has_associated_traits_value
				}
				if = {
					limit = { has_trait = drunkard }
					add = task_contract_weight_bonus_employer_has_associated_traits_value
				}
				if = {
					limit = { has_trait = lifestyle_reveler }
					add = task_contract_weight_bonus_employer_has_associated_traits_value
				}
				# Weight down — strong oversight traits (attentive/vigilant hosts).
				if = {
					limit = { has_trait = patient }
					add = task_contract_weight_malus_employer_has_associated_traits_value
				}
				if = {
					limit = { has_trait = diligent }
					add = task_contract_weight_malus_employer_has_associated_traits_value
				}
				if = {
					limit = { has_trait = just }
					add = task_contract_weight_malus_employer_has_associated_traits_value
				}
				if = {
					limit = { has_trait = temperate }
					add = task_contract_weight_malus_employer_has_associated_traits_value
				}
				# Restrictions.
				min = task_contract_weight_employer_associated_traits_min_value
				max = task_contract_weight_employer_associated_traits_max_value
			}
			multiply = task_contract_weight_by_tier_value
			# Contact list weight — placed at the bottom as override.
			add = task_contract_weight_employer_contact_list_value
		}
		# Rarity modifier — very rare contract.
		add = task_contract_weight_interesting_very_rare_value
	}

	# Validity Triggers
	valid_to_create = {
		# Game rule gate — respect lewd contract availability setting.
		regula_lewd_contracts_available_trigger = yes
		# Standard triggers.
		valid_laamp_basic_trigger = {
			EMPLOYER = scope:employer
			LAAMP = root
		}
		valid_laamp_sensible_start_trigger = yes
		# Employer must be hosting a valid activity (supported type, not yet complete).
		# No treasury requirement — this is a self-motivated crime, not a hired job.
		exists = scope:employer.involved_activity
		scope:employer = { this = scope:employer.involved_activity.activity_host }
		scope:employer.involved_activity = {
			regula_maiden_hunt_valid_activity_trigger = yes
		}
		# Must have at least 1 eligible woman attending the activity.
		# (Filler women are generated at arrival to supplement thin pools.)
		scope:employer.involved_activity = {
			any_attending_character = {
				count >= 1
				regula_maiden_hunt_valid_target_woman_trigger = yes
			}
		}
	}
	valid_to_accept = {
		# Standard triggers.
		valid_laamp_basic_accept_only_trigger = yes
		valid_laamp_basic_trigger = {
			EMPLOYER = scope:employer
			LAAMP = root
		}
		NOT = {
			house = {
				has_house_modifier = legacy_adventurer_house_modifier
			}
		}
		is_adult = yes
		OR = {
			is_male = yes
			carn_is_futa_trigger = yes
		}
	}
	valid_to_continue = {
		# Standard triggers — player alive and adult.
		valid_laamp_basic_trigger = {
			EMPLOYER = root.task_contract_employer
			LAAMP = root.task_contract_taker
		}
		root.task_contract_taker = {
			NOT = {
				house = {
					has_house_modifier = legacy_adventurer_house_modifier
				}
			}
		}
		task_contract_taker = { is_adult = yes }
		# Activity must still exist — once it ends, this fails and on_invalidated fires.
		exists = var:target_activity
	}
	valid_to_keep = {
		# Same as valid_to_continue.
		valid_laamp_basic_trigger = {
			EMPLOYER = root.task_contract_employer
			LAAMP = root.task_contract_taker
		}
		task_contract_taker = { is_adult = yes }
		exists = var:target_activity
	}

	# On_Actions
	on_create = {
		scope:contract = {
			save_scope_as = task_contract

			# Store the target activity from the employer's involved activity.
			task_contract_employer = {
				save_scope_as = employer
			}
			scope:employer.involved_activity = {
				save_scope_as = target_activity
			}

			# Store activity reference.
			set_variable = {
				name = target_activity
				value = scope:target_activity
			}

			# Store activity type flag (one of eight, mutually exclusive).
			scope:target_activity = {
				switch = {
					trigger = has_activity_type
					activity_feast = {
						scope:task_contract = { set_variable = { name = activity_type_feast value = yes } }
					}
					activity_wedding = {
						scope:task_contract = { set_variable = { name = activity_type_wedding value = yes } }
					}
					activity_tournament = {
						scope:task_contract = { set_variable = { name = activity_type_tournament value = yes } }
					}
					activity_coronation = {
						scope:task_contract = { set_variable = { name = activity_type_coronation value = yes } }
					}
					activity_funeral = {
						scope:task_contract = { set_variable = { name = activity_type_funeral value = yes } }
					}
					activity_hunt = {
						scope:task_contract = { set_variable = { name = activity_type_hunt value = yes } }
					}
					activity_imperial_examination = {
						scope:task_contract = { set_variable = { name = activity_type_examination value = yes } }
					}
					activity_local_examination = {
						scope:task_contract = { set_variable = { name = activity_type_examination value = yes } }
					}
					activity_adult_education = {
						scope:task_contract = { set_variable = { name = activity_type_university_visit value = yes } }
					}
				}
			}

			# Store activity location (province).
			set_variable = {
				name = activity_location
				value = scope:target_activity.activity_location
			}

			# Per-kidnap gold starts at 0 — rewards are granted per individual capture.
			set_variable = {
				name = kidnap_gold
				value = 0
			}
		}
	}

	on_accepted = {
		task_contract_taker = { play_sound_effect = "event:/DLC/EP3/SFX/UI/Contracts/ep3_ui_contracts_accept_contract" }
		# Save scopes for the intro event.
		save_scope_as = task_contract
		task_contract_employer = { save_scope_as = activity_host }
		# Initialize counters.
		set_variable = {
			name = difficulty_level
			value = 0
		}
		# Trigger intro event on the taker.
		task_contract_taker ?= {
			trigger_event = regula_maiden_hunt_contract.0001
		}
	}

	on_invalidated = {
		if = {
			limit = {
				root.task_contract_taker = {
					any_character_active_contract = {
						this = root
					}
				}
			}
			# Save scopes for invalidation handling.
			save_scope_as = task_contract
			task_contract_employer = { save_scope_as = task_contract_employer }

			task_contract_taker = {
				set_variable = {
					name = invalidated_task_contract_type
					value = flag:laamp_regula_maiden_hunt
				}
			}

			# Determine outcome based on player state.
			if = {
				limit = {
					exists = var:player_arrived
					variable_list_size = { name = kidnapped_women value >= 1 }
				}
				# Player arrived and kidnapped at least one target.
				if = {
					limit = { exists = var:special_target_kidnapped }
					# Special target was kidnapped — critical success.
					regula_maiden_hunt_cleanup_effect = yes
					complete_task_contract = success_critical
				}
				else = {
					# Standard kidnappings only — standard success.
					regula_maiden_hunt_cleanup_effect = yes
					complete_task_contract = success_standard
				}
			}
			else_if = {
				limit = {
					exists = var:player_arrived
					NOT = { variable_list_size = { name = kidnapped_women value >= 1 } }
				}
				# Player arrived but kidnapped no one — clean invalidation.
				regula_maiden_hunt_cleanup_effect = yes
			}
			else = {
				# Player never arrived — clean invalidation.
				regula_maiden_hunt_cleanup_effect = yes
			}
		}
	}

	# Rewards
	task_contract_reward = {
		success_critical = {
			should_print_on_complete = yes
			effect = {
				task_contract_taker = { play_sound_effect = "event:/DLC/EP3/SFX/UI/Contracts/ep3_ui_contracts_success_on_contract" }
				save_scope_as = task_contract
				# Camp building extra Criminal Rewards: +50%.
				if = {
					limit = {
						task_contract_taker.domicile ?= { has_domicile_parameter = camp_improved_criminal_contract_rewards }
					}
					save_scope_value_as = { name = extra_reward value = flag:yes }
				}
				else = {
					save_scope_value_as = { name = extra_reward value = flag:no }
				}
				laamp_rewards_disburse_ordinary_currency_effect = {
					GOLD = scope:task_contract.var:gold_success_critical
					PRESTIGE = task_contract_success_prestige_gain_full_value
					PIETY = 0
					PROVISIONS = task_contract_success_provisions_gain_major_value
					OPINION = task_contract_opinion_excelled_reward_value
					OPINION_TYPE = succeeded_criminal_contract_against_me_opinion
					CONTACT = no
					CONTACT_HOOK = no
					EXTRA_REWARD = scope:extra_reward
				}
				# Update gallowsbait score — bandit track.
				task_contract_taker = {
					laamp_rewards_apply_criminal_xp_effect = {
						TRACK = bandit
						XP_MAX = gallowsbait_xp_major_gain
						XP_MIN = gallowsbait_xp_major_gain
					}
				}
				# Track regula contract completion.
				task_contract_regula_contract_tracker_effect = yes
			}
		}
		success_standard = {
			should_print_on_complete = yes
			effect = {
				task_contract_taker = { play_sound_effect = "event:/DLC/EP3/SFX/UI/Contracts/ep3_ui_contracts_success_on_contract" }
				save_scope_as = task_contract
				# Camp building extra Criminal Rewards: +50%.
				if = {
					limit = {
						task_contract_taker.domicile ?= { has_domicile_parameter = camp_improved_criminal_contract_rewards }
					}
					save_scope_value_as = { name = extra_reward value = flag:yes }
				}
				else = {
					save_scope_value_as = { name = extra_reward value = flag:no }
				}
				laamp_rewards_disburse_ordinary_currency_effect = {
					GOLD = scope:task_contract.var:gold_success_standard
					PRESTIGE = task_contract_success_prestige_gain_half_value
					PIETY = 0
					PROVISIONS = task_contract_success_provisions_gain_medium_value
					OPINION = task_contract_opinion_standard_reward_value
					OPINION_TYPE = succeeded_criminal_contract_against_me_opinion
					CONTACT = no
					CONTACT_HOOK = no
					EXTRA_REWARD = scope:extra_reward
				}
				# Update gallowsbait score — bandit track.
				task_contract_taker = {
					laamp_rewards_apply_criminal_xp_effect = {
						TRACK = bandit
						XP_MAX = gallowsbait_xp_medium_gain
						XP_MIN = gallowsbait_xp_medium_gain
					}
				}
				# Track regula contract completion.
				task_contract_regula_contract_tracker_effect = yes
			}
		}
		failure_standard = {
			positive = no
			should_print_on_complete = yes
			effect = {
				task_contract_taker = { play_sound_effect = "event:/DLC/EP3/SFX/UI/Contracts/ep3_ui_contracts_failure_on_contract" }
				save_scope_as = task_contract
				save_scope_value_as = { name = extra_reward value = flag:no }
				# Standard Criminal Penalties.
				laamp_rewards_disburse_ordinary_currency_effect = {
					GOLD = 0
					PRESTIGE = task_contract_failure_prestige_loss_crime_value
					PIETY = 0
					PROVISIONS = task_contract_failure_provisions_gain_minor_value
					OPINION = task_contract_opinion_crime_failure_value
					OPINION_TYPE = attempted_criminal_contract_against_me_opinion
					CONTACT = no
					CONTACT_HOOK = no
					EXTRA_REWARD = scope:extra_reward
				}
				# Update gallowsbait score — bandit track (reduced on failure).
				task_contract_taker = {
					laamp_rewards_apply_criminal_xp_effect = {
						TRACK = bandit
						XP_MAX = gallowsbait_xp_minor_gain
						XP_MIN = gallowsbait_xp_minor_gain
					}
				}
			}
		}
	}
}
